package com.yunc.user.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.common.core.result.R;
import com.yunc.user.sys.entity.SysMessage;
import com.yunc.user.sys.entity.SysMessageUser;
import com.yunc.user.sys.entity.vo.SysMessageSearchVO;
import com.yunc.user.sys.service.ISysMessageService;
import com.yunc.user.sys.service.ISysMessageUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 消息
 *
 * @author congcong tao
 * @create 2020/1/14 14:36
 */
@RestController
@RequestMapping("sysMessage")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysMessageController {

    private final ISysMessageService sysMessageService;
    private final ISysMessageUserService sysMessageUserService;

    /**
     * 分页数据
     */
    @GetMapping("/page")
    public R page(Page<SysMessage> page, String userId, SysMessageSearchVO sysMessage) {
        sysMessage.setUserId(userId);
        List<SysMessage> sysMessageList = sysMessageService.pageInfo(page, sysMessage);
        return R.ok().put("data", sysMessageList).put("count", page.getTotal());
    }

    /**
     * 查询list
     */
    @GetMapping("/listSize")
    public R listSize(String size) {
        int limit = 5;
        if (StringUtils.isNotEmpty(size)) {
            limit = Integer.parseInt(size);
        }
        Page<SysMessage> page = new Page<>();
        page.setCurrent(0);
        page.setSize(limit);
        sysMessageService.page(page, new QueryWrapper<SysMessage>().orderByDesc("create_time"));
        return R.ok().put("data", page.getRecords());
    }

    /**
     * 当前未读消息
     */
    @GetMapping("/countNum")
    public R countNum(String userId) {
        // 未读消息
        int countNum = sysMessageService.getCountUser(userId, "1");
        return R.ok().put("countNum", countNum);
    }

    /**
     * 消息内容
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable String id) {
        SysMessage message = sysMessageService.getById(id);
        return R.ok().put("sysMessage", message);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(SysMessage message) {
        // 创建者
        return sysMessageService.saveMessage(message);
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(SysMessage message) {
        boolean flag = sysMessageService.updateById(message);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 更新消息人员表状态
     */
    @PutMapping(value = "/updateStatus")
    public R updateStatus(@NotEmpty String messageUserId, @NotEmpty String status) {
        SysMessageUser sysMessageUser = sysMessageUserService.getById(messageUserId);
        sysMessageUser.setStatus(status);
        boolean flag = sysMessageUserService.updateById(sysMessageUser);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 更新人员所有消息状态
     */
    @PutMapping(value = "/updateStatusByUser")
    public R updateStatus(@NotEmpty String status) {
        SysMessageUser sysMessageUser = new SysMessageUser();
        sysMessageUser.setStatus(status);
        QueryWrapper<SysMessageUser> qw = new QueryWrapper<>();
        // qw.eq("user_id", userId);
        // 原状态修改
        /*if (StringUtils.equals(status, BaseConstant.SYS_MESSAGE_READ)) {
            qw.eq("status", BaseConstant.SYS_MESSAGE_NO_READ);
        }
        if (StringUtils.equals(status, BaseConstant.SYS_MESSAGE_DEL)) {
            qw.eq("status", BaseConstant.SYS_MESSAGE_READ);
        }*/
        boolean flag = sysMessageUserService.update(sysMessageUser, qw);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable String ids) {
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = sysMessageService.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
